package com.qf.xiaomi.web.servlet.admin;

import com.qf.xiaomi.pojo.TbUser;
import com.qf.xiaomi.service.impl.userServiceImpl;
import com.qf.xiaomi.service.userService;
import com.qf.xiaomi.utils.StringConstans;
import com.qf.xiaomi.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * Created by 田贵宾 on 2022/8/21 15:59
 *
 * @author 田贵宾
 * @version 1.0
 * //TODO java练习阶段
 * @since 1.0
 */
@WebServlet("/adminLogin")
public class AdminLoginServlet extends HttpServlet {
    userService service=new userServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        TbUser tbUser = service.IsAdmin(username);
        boolean isAdmin=(tbUser.getRole()==0);
        if (isAdmin) {
            TbUser user = service.login(username, password);
            req.getSession().setAttribute("AdminUesr",user);
            if (user!=null){
                req.getSession().setAttribute("admin",user);
                resp.sendRedirect("/admin/admin.jsp");
                return;
            }else {
                req.setAttribute("exp","登陆失败");
                req.getRequestDispatcher("/error/error.jsp").forward(req,resp);
            }
        }else {
            req.setAttribute("exp","您不是管理员");
            req.getRequestDispatcher("/error/error.jsp").forward(req,resp);
        }


    }
}
